package com.hkts.beltweigher.salve.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.beltweigher.opc.DataCache;
import com.hkts.beltweigher.opc.InstrumentStatusEnum;
import com.hkts.beltweigher.salve.entity.EastBeltscalesCumulativedata;
import com.hkts.beltweigher.salve.entity.EastBeltscalesInstantaneousdata;
import com.hkts.beltweigher.salve.mapper.EastBeltscalesInstantaneousdataMapper;
import com.hkts.beltweigher.salve.service.IEastBeltscalesCumulativedataService;
import com.hkts.beltweigher.salve.service.IEastBeltscalesInstantaneousdataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hml
 * @since 2024-11-18
 */
@Service("iEastBeltscalesInstantaneousdataService")
@Transactional
public class EastBeltscalesInstantaneousdataServiceImpl extends ServiceImpl<EastBeltscalesInstantaneousdataMapper, EastBeltscalesInstantaneousdata> implements IEastBeltscalesInstantaneousdataService {


    @Autowired
    private EastBeltscalesInstantaneousdataMapper eastBeltscalesInstantaneousdataMapper;
    @Autowired
    public IEastBeltscalesCumulativedataService iEastBeltscalesCumulativedataService;

    @Override
    public IPage<EastBeltscalesInstantaneousdata> selectPage(Page<EastBeltscalesInstantaneousdata> page, EastBeltscalesInstantaneousdata eastBeltscalesInstantaneousdata) {
        QueryWrapper<EastBeltscalesInstantaneousdata> query = Wrappers.query();
        return eastBeltscalesInstantaneousdataMapper.selectPage(page, query);
    }


    @Override
    public List<EastBeltscalesInstantaneousdata> selectList(EastBeltscalesInstantaneousdata eastBeltscalesInstantaneousdata) {
        QueryWrapper<EastBeltscalesInstantaneousdata> query = Wrappers.query();
        return eastBeltscalesInstantaneousdataMapper.selectList(query);
    }

    @Override
    public EastBeltscalesInstantaneousdata selectByLastData() {
        QueryWrapper<EastBeltscalesInstantaneousdata> query = Wrappers.query();
        query.orderByDesc();
        return eastBeltscalesInstantaneousdataMapper.selectOne(query);
    }

    @Override
    public void handlerInstantaneousdata() {
        //当前的小时值
        LocalDateTime now = LocalDateTime.now();
        int currentHour =now.getHour();
        //查询上一条记录
        EastBeltscalesInstantaneousdata instantaneousdata = eastBeltscalesInstantaneousdataMapper.selectById(1);
        int lastHour =  instantaneousdata.getDatetime().getHour();

        instantaneousdata.setDatetime(now);
        instantaneousdata.setDate(LocalDate.now());
        instantaneousdata.setTime(LocalDateTime.now().toLocalTime());
        // instantaneousdata.setMs( LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
        //瞬时流量
        instantaneousdata.setInstantaneousTraffic1(Float.parseFloat(DataCache.ALLCACHE.get("6").toString()));
        instantaneousdata.setInstantaneousTraffic2(Float.parseFloat(DataCache.ALLCACHE.get("7").toString()));
        instantaneousdata.setInstantaneousTraffic3(Float.parseFloat(DataCache.ALLCACHE.get("8").toString()));
        instantaneousdata.setInstantaneousTraffic4(Float.parseFloat(DataCache.ALLCACHE.get("9").toString()));
        instantaneousdata.setInstantaneousTraffic5(Float.parseFloat(DataCache.ALLCACHE.get("10").toString()));
        //设置流量
        instantaneousdata.setSetValue1(Float.parseFloat(DataCache.ALLCACHE.get("1").toString()));
        instantaneousdata.setSetValue2(Float.parseFloat(DataCache.ALLCACHE.get("2").toString()));
        instantaneousdata.setSetValue3(Float.parseFloat(DataCache.ALLCACHE.get("3").toString()));
        instantaneousdata.setSetValue4(Float.parseFloat(DataCache.ALLCACHE.get("4").toString()));
        instantaneousdata.setSetValue5(Float.parseFloat(DataCache.ALLCACHE.get("5").toString()));

        //累计流量
        instantaneousdata.setCumulativeFlow1(Float.parseFloat(DataCache.ALLCACHE.get("11").toString()));
        instantaneousdata.setCumulativeFlow2(Float.parseFloat(DataCache.ALLCACHE.get("12").toString()));
        instantaneousdata.setCumulativeFlow3(Float.parseFloat(DataCache.ALLCACHE.get("13").toString()));
        instantaneousdata.setCumulativeFlow4(Float.parseFloat(DataCache.ALLCACHE.get("14").toString()));
        instantaneousdata.setCumulativeFlow5(Float.parseFloat(DataCache.ALLCACHE.get("15").toString()));

        //皮带秤速度
        instantaneousdata.setSpeed1(Float.parseFloat(DataCache.ALLCACHE.get("16").toString()));
        instantaneousdata.setSpeed2(Float.parseFloat(DataCache.ALLCACHE.get("17").toString()));
        instantaneousdata.setSpeed3(Float.parseFloat(DataCache.ALLCACHE.get("18").toString()));
        instantaneousdata.setSpeed4(Float.parseFloat(DataCache.ALLCACHE.get("19").toString()));
        instantaneousdata.setSpeed5(Float.parseFloat(DataCache.ALLCACHE.get("20").toString()));
        //当前小时累计值
        if(lastHour!=currentHour){
            //小时累计值
            instantaneousdata.setHourFlow1((float) 0);
            instantaneousdata.setHourFlow2((float) 0);
            instantaneousdata.setHourFlow3((float) 0);
            instantaneousdata.setHourFlow4((float) 0);
            instantaneousdata.setHourFlow5((float) 0);
           //小时累开始计值
            instantaneousdata.setHourStartFlow1(Float.parseFloat(DataCache.ALLCACHE.get("11").toString()));
            instantaneousdata.setHourStartFlow2(Float.parseFloat(DataCache.ALLCACHE.get("12").toString()));
            instantaneousdata.setHourStartFlow3(Float.parseFloat(DataCache.ALLCACHE.get("13").toString()));
            instantaneousdata.setHourStartFlow4(Float.parseFloat(DataCache.ALLCACHE.get("14").toString()));
            instantaneousdata.setHourStartFlow5(Float.parseFloat(DataCache.ALLCACHE.get("15").toString()));
        }else{
            //计算差值
            instantaneousdata.setHourFlow1(Float.parseFloat(DataCache.ALLCACHE.get("11").toString())-instantaneousdata.getHourStartFlow1());
            instantaneousdata.setHourFlow2(Float.parseFloat(DataCache.ALLCACHE.get("12").toString())-instantaneousdata.getHourStartFlow2());
            instantaneousdata.setHourFlow3(Float.parseFloat(DataCache.ALLCACHE.get("13").toString())-instantaneousdata.getHourStartFlow3());
            instantaneousdata.setHourFlow4(Float.parseFloat(DataCache.ALLCACHE.get("14").toString())-instantaneousdata.getHourStartFlow4());
            instantaneousdata.setHourFlow5(Float.parseFloat(DataCache.ALLCACHE.get("15").toString())-instantaneousdata.getHourStartFlow5());
        }
        saveOrUpdate(instantaneousdata);
        //计算批次数据

        List<EastBeltscalesCumulativedata> list=new ArrayList<>();
        //仪表1
        if(instantaneousdata.getSpeed1()==0){
            String status = getStatus(InstrumentStatusEnum.instrument1key);
            //瞬时流量为0且数据是启动状态时需要修改状态
            if(status.equals(DataCache.STARTUP)){
                EastBeltscalesCumulativedata instrument1  = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument1key));
                instrument1.setEndTime(now);
                list.add(instrument1);
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument1key,DataCache.STOP);
            }
        }else{

            String status = getStatus(InstrumentStatusEnum.instrument1key);
            if(status.equals(DataCache.STOP)){
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument1key,DataCache.STARTUP);
            }
            EastBeltscalesCumulativedata byName = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument1key));
            if(byName==null){
                byName=new EastBeltscalesCumulativedata();
                byName.setName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument1key));
                byName.setStartTime(now);
                byName.setStartValue(Float.parseFloat(DataCache.ALLCACHE.get("11").toString()));
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("11").toString()));
                byName.setTotalValue((float)0);
            }else{
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("11").toString()));
                byName.setTotalValue(byName.getEndValue()-byName.getStartValue());
            }
            list.add(byName);
        }
        //仪表2
        if(instantaneousdata.getSpeed2()==0){
            String status = getStatus(InstrumentStatusEnum.instrument2key);
            //瞬时流量为0且数据是启动状态时需要修改状态
            if(status.equals(DataCache.STARTUP)){
                EastBeltscalesCumulativedata instrument2  = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument2key));
                instrument2.setEndTime(now);
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument2key,DataCache.STOP);
                list.add(instrument2);
            }
        }else {
            String status = getStatus(InstrumentStatusEnum.instrument2key);
            if (status.equals(DataCache.STOP)) {
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument2key, DataCache.STARTUP);
            }
            EastBeltscalesCumulativedata byName = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument2key));
            if (byName == null) {
                byName = new EastBeltscalesCumulativedata();
                byName.setName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument2key));
                byName.setStartTime(now);
                byName.setStartValue(Float.parseFloat(DataCache.ALLCACHE.get("12").toString()));
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("12").toString()));
                byName.setTotalValue((float) 0);
            } else {
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("12").toString()));
                byName.setTotalValue(byName.getEndValue() - byName.getStartValue());
            }
            list.add(byName);
        }
        //仪表3
        if(instantaneousdata.getSpeed3()==0){
            String status = getStatus(InstrumentStatusEnum.instrument3key);
            //瞬时流量为0且数据是启动状态时需要修改状态
            if(status.equals(DataCache.STARTUP)){
                EastBeltscalesCumulativedata instrument3  = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument3key));
                instrument3.setEndTime(now);
                list.add(instrument3);
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument3key,DataCache.STOP);
            }
        }else {
            String status = getStatus(InstrumentStatusEnum.instrument3key);
            if (status.equals(DataCache.STOP)) {
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument3key, DataCache.STARTUP);
            }
            EastBeltscalesCumulativedata byName = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument3key));
            if (byName == null) {
                byName = new EastBeltscalesCumulativedata();
                byName.setName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument3key));
                byName.setStartTime(now);
                byName.setStartValue(Float.parseFloat(DataCache.ALLCACHE.get("13").toString()));
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("13").toString()));
                byName.setTotalValue((float) 0);
            } else {
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("13").toString()));
                byName.setTotalValue(byName.getEndValue() - byName.getStartValue());
            }
            list.add(byName);
        }
       //仪表4
        if(instantaneousdata.getSpeed4()==0){
            String status = getStatus(InstrumentStatusEnum.instrument4key);
            //瞬时流量为0且数据是启动状态时需要修改状态
            if(status.equals(DataCache.STARTUP)){
                EastBeltscalesCumulativedata instrument4  = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument4key));
                instrument4.setEndTime(now);
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument4key,DataCache.STOP);
                list.add(instrument4);
            }
        }else {
            String status = getStatus(InstrumentStatusEnum.instrument4key);
            if (status.equals(DataCache.STOP)) {
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument4key, DataCache.STARTUP);
            }
            EastBeltscalesCumulativedata byName = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument4key));
            if (byName == null) {
                byName = new EastBeltscalesCumulativedata();
                byName.setName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument4key));
                byName.setStartTime(now);
                byName.setStartValue(Float.parseFloat(DataCache.ALLCACHE.get("14").toString()));
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("14").toString()));
                byName.setTotalValue((float) 0);
            } else {
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("14").toString()));
                byName.setTotalValue(byName.getEndValue() - byName.getStartValue());
            }
            list.add(byName);

        }
        //仪表5
        if(instantaneousdata.getSpeed5()==0){
            String status = getStatus(InstrumentStatusEnum.instrument5key);
            //瞬时流量为0且数据是启动状态时需要修改状态
            if(status.equals(DataCache.STARTUP)){
                EastBeltscalesCumulativedata instrument5  = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument5key));
                instrument5.setEndTime(now);
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument5key,DataCache.STOP);
                list.add(instrument5);
            }
        }else {
            String status = getStatus(InstrumentStatusEnum.instrument5key);
            if (status.equals(DataCache.STOP)) {
                DataCache.INSTRUMENT_STATUS_CACHE.put(InstrumentStatusEnum.instrument5key, DataCache.STARTUP);
            }
            EastBeltscalesCumulativedata byName = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument5key));
            if (byName == null) {
                byName = new EastBeltscalesCumulativedata();
                byName.setName(InstrumentStatusEnum.getName(InstrumentStatusEnum.instrument5key));
                byName.setStartTime(now);
                byName.setStartValue(Float.parseFloat(DataCache.ALLCACHE.get("15").toString()));
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("15").toString()));
                byName.setTotalValue((float) 0);
            } else {
                byName.setEndValue(Float.parseFloat(DataCache.ALLCACHE.get("15").toString()));
                byName.setTotalValue(byName.getEndValue() - byName.getStartValue());
            }
            list.add(byName);
        }
        iEastBeltscalesCumulativedataService.saveOrUpdateBatch(list);
    }

    public String getStatus(String key){
        String status = DataCache.INSTRUMENT_STATUS_CACHE.get( key);
        if(status==null){
            EastBeltscalesCumulativedata byName = iEastBeltscalesCumulativedataService.getByName(InstrumentStatusEnum.getName(key));
           if(byName==null){
               DataCache.INSTRUMENT_STATUS_CACHE.put(key,"2");
               status="2";
           }else{
               DataCache.INSTRUMENT_STATUS_CACHE.put(key,"1");
               status="1";
           }
        }
        return status;
    }
}
